package cn.com.note.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatAutoCompleteTextView;
import android.support.v7.widget.AppCompatSpinner;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;

import java.util.ArrayList;
import java.util.List;

import cn.com.note.R;
import cn.com.note.adapter.AutoTextAdapter;
import cn.com.note.helper.SwipeSpinnerHelper;

/**
 * 下拉选择 Demo
 * Created by SunFuLin on 2019/1/4.
 */

public class SelectViewActivity extends AppCompatActivity implements View.OnClickListener {
    boolean isEnable = true;
    AppCompatSpinner mSpinnerSimple;
    RecyclerView rvYear, rvMonth, rvDate;
    AppCompatAutoCompleteTextView autoTV;
    AutoTextAdapter arrayAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_view);
        findViewById(R.id.bt_enable).setOnClickListener(this);
        findViewById(R.id.bt_focus).setOnClickListener(this);
        findViewById(R.id.send_item).setOnClickListener(this);
        findViewById(R.id.bt_setv).setOnClickListener(this);
        rvYear = findViewById(R.id.year);
        rvMonth = findViewById(R.id.month);
        rvDate = findViewById(R.id.date);
        autoTV = findViewById(R.id.autoText);
        SwipeSpinnerHelper.bindRecyclerView(rvYear);
        SwipeSpinnerHelper.bindRecyclerView(rvMonth);
        SwipeSpinnerHelper.bindRecyclerView(rvDate);
        mSpinnerSimple = findViewById(R.id.spinner);
        initSpinner();
        initRecycle();
        initAutoText();
        LogUtils.log(Log.INFO, "内容：", "SelectViewActivity 页启动");
    }

    private void initAutoText() {
        final String[] arr = {"aa", "aab", "aac", "bb", "b", "bbc"};
        arrayAdapter = new AutoTextAdapter(this, R.layout.item_auto_search, arr);
        autoTV.setAdapter(arrayAdapter);
        autoTV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String text = autoTV.getText().toString();
                Toast.makeText(SelectViewActivity.this, "" + text, Toast.LENGTH_SHORT).show();
            }
        });
        autoTV.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
//                autoTV.showDropDown();//显示下拉列表
                return false;
            }
        });
    }

    private void initRecycle() {
        List<String> listYear = new ArrayList<>();
        List<String> listMonth = new ArrayList<>();
        List<String> listDate = new ArrayList<>();
        for (int i = 1; i < 32; i++) {
            listYear.add(String.valueOf(1999 + i));
            listDate.add(String.valueOf(i));
            if (i < 13) {
                listMonth.add(String.valueOf(i));
            }
        }
        rvYear.setLayoutManager(new LinearLayoutManager(this));
        rvYear.setAdapter(new MyAdapter(this, listYear));
        rvMonth.setLayoutManager(new LinearLayoutManager(this));
        rvMonth.setAdapter(new MyAdapter(this, listMonth));
        rvDate.setLayoutManager(new LinearLayoutManager(this));
        rvDate.setAdapter(new MyAdapter(this, listDate));
    }

    private void initSpinner() {
        //原始 string 数组
        final String[] spinnerItems = {
                "张三胜多负少说法犯得上发射点发射点sfdadsfasdfasdfasdfasdfasdf发", "李四手动阀阀手动阀", "王二麻阀手动阀子",
                "张三上发射点发", "李asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfadsfasdfasdfasdf四动阀", "王二麻啊手子",
                "张三胜多点发", "李四手动阀手动阀手动阀手动阀", "王二麻阀手动阀子",
                "张三胜多负", "李四手动阀手动阀手动阀手", "王二麻啊手动阀子",
                "张三胜多负少发", "李四手动阀", "王二阀子",
                "张三发射点发", "李四阀", "王二麻啊手动子"
        };
        mSpinnerSimple.setPrompt("测试标题内容");

        //简单的string数组适配器：样式res，数组
        ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
                R.layout.simple_spinner_dropdown_item, spinnerItems);
        //下拉的样式res
        //spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //绑定 Adapter到控件
        mSpinnerSimple.setAdapter(spinnerAdapter);
        //选择监听
        mSpinnerSimple.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            //parent就是父控件spinner
            //view就是spinner内填充的textview,id=@android:id/text1
            //position是值所在数组的位置
            //id是值所在行的位置，一般来说与positin一致
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int pos, long id) {
                Toast.makeText(SelectViewActivity.this, "选择了[" + spinnerItems[pos] + "]", Toast.LENGTH_SHORT).show();
                //设置spinner内的填充文字居中
                //((TextView)view).setGravity(Gravity.CENTER);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_enable:
                isEnable = !isEnable;
                mSpinnerSimple.setEnabled(isEnable);
                break;
            case R.id.bt_focus:
                mSpinnerSimple.setFocusableInTouchMode(true);
                mSpinnerSimple.setFocusable(true);
                mSpinnerSimple.requestFocus();
                break;
            case R.id.send_item:
                Toast.makeText(this, "发货！", Toast.LENGTH_SHORT).show();
                break;
            case R.id.bt_setv:
                String aa="";
                autoTV.setText(aa);
                autoTV.setSelection(aa.length());//将光标移至文字末尾
                break;
        }
    }


    class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {
        private List<String> datas;
        private Context context;

        public MyAdapter(Context context, List<String> datas) {
            this.context = context;
            this.datas = datas;
        }

        @NonNull
        @Override
        public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = View.inflate(context, R.layout.item_recy, null);
            return new MyHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
            myHolder.textView.setText(datas.get(i));
        }

        @Override
        public int getItemCount() {
            return datas.size();
        }

        class MyHolder extends RecyclerView.ViewHolder {
            TextView textView;

            public MyHolder(@NonNull View itemView) {
                super(itemView);
                textView = itemView.findViewById(R.id.text);
            }
        }
    }
}
